//
//  EmotionKeyBordButtons.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/29.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

//底部按钮的枚举
enum ButtonType:Int
{
    case defaultType = 0
    case emojiType = 1
    case xlhType = 2
}


class EmotionKeyBordButtons: UIView
{

    
    //定义一个button，记录一下当前被点击的是哪一个button
    var currentClickButton:UIButton?
    
    
    //定义一个闭包，让其父类去做按钮触发事件
    var buttonClosure:((type:ButtonType) -> ())?
    
    
    
    //懒加载底部的3个button
    //(1)默认表情
    private lazy var defaultButton:UIButton = {
       
        
        let button = UIButton()
        
        button.setTitle("默认", forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_left_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_left_selected"), forState: UIControlState.Selected)
        
        //添加触发事件
        button.addTarget(self, action: #selector(EmotionKeyBordButtons.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置button的tag
        button.tag = ButtonType.defaultType.rawValue
        
        
        return button
    }()
    
    //(2)emoji表情
    private lazy var emojiButton:UIButton = {
        
        
        let button = UIButton()
        
        button.setTitle("emoji", forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Selected)
        
        //添加触发事件
        button.addTarget(self, action: #selector(EmotionKeyBordButtons.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置button的tag
        button.tag = ButtonType.emojiType.rawValue
        
        
        
        return button
    }()
    
    //(3)小浪花
    private lazy var xlhButton:UIButton = {
       
        
        let button = UIButton()
        
        button.setTitle("浪小花", forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_right_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_right_selected"), forState: UIControlState.Selected)
        
        
        //添加触发事件
        button.addTarget(self, action: #selector(EmotionKeyBordButtons.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置button的tag
        button.tag = ButtonType.xlhType.rawValue
        
        
        return button
        
    }()
    
    
    
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        //布局界面
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //布局界面
    func setUpUI()
    {
        
        //将三个按钮添加到主界面上
        addSubview(self.defaultButton)
        addSubview(self.emojiButton)
        addSubview(self.xlhButton)
        
        //设置3个button的约束
        self.defaultButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(self.emojiButton)
        }
        
        self.emojiButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(self.defaultButton.snp_trailing)
            make.width.equalTo(self.xlhButton)
        }
        
        self.xlhButton.snp_makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(self.emojiButton.snp_trailing)
        }
        
        //默认情况下，默认按钮被点击
        self.setUpCurrentButton(self.defaultButton)
        
    }
    
    //按钮的触发事件
    func buttonClick(button:UIButton)
    {
        
        //设置被点击button的状态
        setUpCurrentButton(button)
        
        
        buttonClosure?(type:ButtonType(rawValue: button.tag)!)
    }
    
    //设置被点击button的状态
    func setUpCurrentButton(button:UIButton)
    {
        
        //避免当前的button被多次点击
        if button == self.currentClickButton
        {
            return
        }
        self.currentClickButton?.selected = false
        
        //设置button被点击
        button.selected = true
        //更新button
        self.currentClickButton = button
    }
    
    //外接给外部，从外部改变button的状态
    func changeButtonStatusWithSection(section:Int)
    {
        //根据传递过来的section信息，装换成button的tag信息，在根据tag信息转换成响应的button控件
        
        if section == 0
        {
            setUpCurrentButton(self.defaultButton)
        }
        
        else
        {
            //注意!!!!!!!,当tag == 0时，不能直接利用viewWithTag转换view
            let button = viewWithTag(section) as! UIButton
            
            setUpCurrentButton(button)
        }
    }
    

}
